package top.ysh;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

/*
    Jedis工具类
 */
public class JedisUtils {

    // 总数
    private static int maxTotal;
    // 最大连接数
    private static int maxIdel;
    // 主机ip
    private static String host;
    // 端口号
    private static int port;
    // 连接池配置对象
    private static JedisPoolConfig jpc;
    // 连接池对象
    private static JedisPool jp;

    static {
        /*
           ResourceBundle：读取properties文件的工具类
               要求：
                    1. properties文件必须存放在classpath（src或java）目录下
                    2. 读取时，只需要properties的文件名称

         */
        ResourceBundle bundle = ResourceBundle.getBundle("redis");
        maxTotal = Integer.parseInt(bundle.getString("redis.maxTotal"));
        maxIdel = Integer.parseInt(bundle.getString("redis.maxIdel"));
        host = bundle.getString("redis.host");
        port = Integer.parseInt(bundle.getString("redis.port"));
        jpc = new JedisPoolConfig();
        jp = new JedisPool(jpc, host, port);
    }

    public static Jedis getJedis() {
        return jp.getResource();
    }
}
